<?php
namespace app\backend\controller;

use app\backend\model\Joinus as JoinusModel;
use app\backend\model\Qualification as QualificationModel;
use app\backend\model\Qualification;
use app\backend\model\Responsiblities as ResponsiblitiesModel;
use app\backend\model\Menu as MenuModel;
use think\Db;
use think\Request;
use app\backend\org\Helper;

class Joinus extends Base
{

    // 加入我们列表展示
    public function index()
    {
        $model = new JoinusModel;
        // 获取分页显示
        $result = $model->getAll()->paginate(5);
        $page = $result->render();
        $this->assign('page',$page);
        $this->assign('joinus',$result);
        $this->assign('empty',"暂无数据");

        //接收资格ajax 传过来的值
        if(Request::instance()->isAjax()){
            $p_id = Request::instance()->post('p_id');// 获取经过过滤的全部post变量
            $idname = Request::instance()->post('idname');// 获取经过过滤的全部post变量

            //echo $idname;exit;
            if($idname=='qu'){
               // echo 112;exit;
                $model1 = new QualificationModel;
                $result1 = $model1->getAll($p_id);
               // var_dump($result1);exit;
                foreach ($result1 as $k=>$v){
                    $result1[$k]["qualification_cn"] = stripslashes($v["qualification_cn"]);
                    $result1[$k]["qualification_en"] = stripslashes($v["qualification_en"]);
                    $result1[$k]["quname_cn"] = mb_substr($v["qualification_cn"],0,7);
                    $result1[$k]["quname_en"] = mb_substr($v["qualification_en"],0,20);
                }
                $data = ['qu'=>$result1];
                return json($data);
            }else if($idname=='re'){
                //接收责任ajax 传过来的值
                $model2 = new ResponsiblitiesModel;
                $result2 = $model2->getAll($p_id);
                foreach ($result2 as $k=>$v){
                    $result2[$k]["responsiblities_cn"] = stripslashes($v["responsiblities_cn"]);
                    $result2[$k]["responsiblities_en"] = stripslashes($v["responsiblities_en"]);
                    $result2[$k]["rename_cn"] = mb_substr($v["responsiblities_cn"],0,7);
                    $result2[$k]["rename_en"] = mb_substr($v["responsiblities_en"],0,20);
                }
                $data2 = ['re'=>$result2];
                return json($data2);
            }
        }

        return $this->fetch();

    }

    //新建加入我们
    public function joinus_create(){
        if (Request::instance()->isPost())
        {
            $model = new JoinusModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量

            // 判断已有内容不能重复新增去修改
            $name = Db::query("select joinus_id,title_cn from ls_joinus where `title_cn`='".$post['title_cn']."' and delete_time is NULL ");
            //var_dump($name);exit;
            if(!empty($name)){
                return $this->redirect(url('joinus_update',array('id'=>$name[0]['joinus_id'])));
            }

            // 验证表单并插入数据库
            $result = $this->validate($post,'Joinus.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
                return $this->error($msg = '参数错误，验证失败');
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');
            //$model->insert($post);

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //var_dump($model);
            //var_dump($line);die();
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect('joinus/index');
            }
        };



        //查询某个分类的直接子类id和名字
        $this->assign('act','index');
        return $this->fetch();
    }

    //修改加入我们
    public function joinus_update($id)
    {
        if (Request::instance()->isPost())
        {
            $model = new JoinusModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量

            //验证表单并插入数据库
            $result = $this->validate($post,'Joinus.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['joinus_id'=>$id]);
            //echo $result;exit;
            if(false === $result){
                $this->error($this->update_error);
            } else {
                $this->redirect('joinus/index');
            }
        }
        //根据主键插记录
        $record = JoinusModel::get($id);
        //var_dump($record);exit;
        $this->assign('id',$id);
        $this->assign('joinus', $record);
        $this->assign('act','index');
        return $this->fetch();
    }

    //删除
    public function joinus_delete($id)
    {
        QualificationModel::destroy(["p_id"=>$id]);
        ResponsiblitiesModel::destroy(["p_id"=>$id]);
        JoinusModel::destroy($id);//软删除
        //AlltimeModel::destroy($id,true);//真实删除

        $this->redirect('joinus/index');
        return $this->fetch();
    }

    //新增资格
    public function qu_create(){
        if (Request::instance()->isAjax())
        {
            $model = new QualificationModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            $post["qualification_cn"]= addslashes($post["qualification_cn"]);
            $post["qualification_en"]= addslashes($post["qualification_en"]);
            // 验证表单并插入数据库
            $result = $this->validate($post,'Qualification.add');
            //验证一定要这种写法不然会失效
            if(true !== $result){
                // 验证失败 输出错误信息
                //var_dump($result);
                return json([
                    "code"=>4001,
                    "message"=>"验证失败，标题不能为空"
                ]);
            }
            //return json(["code"=>$post]);
            //var_dump($post);exit;
            // 判断已有内容不能重复新增去修改
            $name = Db::query("select qu_id,qualification_cn from ls_qualification where`p_id`='".$post['p_id']."'and `qualification_cn`='".$post['qualification_cn']."' and delete_time is NULL ");
            //var_dump($name);exit;
            if(!empty($name)){
                return json([
                    "code"=>4000,
                    "message"=>"该资质已存在"
                ]);
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');
            //$model->insert($post);
            unset($post["quname_cn"],$post["quname_en"]);

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //var_dump($model);
            //var_dump($line);die();
            if(!$line){
                return json([
                    "code"=>4002,
                    "message"=>"添加失败"
                ]);
            } else {
                return json([
                    "code"=>2000,
                    "data"=>['id'=>$model->qu_id],
                    "message"=>"添加成功"
                ]);

            }
        };

    }

    //修改资格
    public function qu_update(){
        if (Request::instance()->isAjax())
        {
            $model = new QualificationModel;
            if(Request::instance()->isPost()){
                $post =Request::instance()->post() ;// 获取经过过滤的全部post变量
                unset($post["quname_cn"],$post["quname_en"]);
                if(!empty($post["qualification_cn"])){
                    $post["qualification_cn"]= addslashes($post["qualification_cn"]);
                    $post["qualification_en"]= addslashes($post["qualification_en"]);
                    //验证表单并插入数据库
                    $result = $this->validate($post,'Qualification.edit');
                    if(true !== $result){
                        return json([
                            "code"=>4001,
                            "message"=>"验证失败，标题不能为空"
                        ]);
                    }
                    // 过滤掉数据表中没有的字段，保存
                    $result = $model->allowField(true)->save($post,['qu_id'=>$post["qu_id"]]);
                    //echo $result;exit;
                    if(false === $result){
                        return json([
                            "code"=>4002,
                            "message"=>"添加失败"
                        ]);
                    } else {
                        return json([
                            "code"=>2000,
                            "message"=>"修改成功"
                        ]);
                    }
                }else{
                    $record = QualificationModel::get($post["qu_id"]);
                    return json($record);
                }
            }
        }
    }

    //删除资格
    public function qu_delete(){
        if (Request::instance()->isAjax())
        {
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            QualificationModel::destroy($post["qu_id"]);

            return json(
            [
                "code"=>2000,
                "message"=>"删除成功"
            ]
            );
        }


    }

    //新增责任
    public function re_create(){
        if (Request::instance()->isAjax())
        {
            $model = new ResponsiblitiesModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            $post["responsiblities_cn"]= addslashes($post["responsiblities_cn"]);
            $post["responsiblities_en"]= addslashes($post["responsiblities_en"]);
            var_dump($post);//exit;
            // 验证表单并插入数据库
            $result = $this->validate($post,'Responsiblities.add');
            //var_dump($result);exit;
            //验证一定要这种写法不然会失效
            if(true !== $result){
                // 验证失败 输出错误信息
                //var_dump($result);
                return json([
                    "code"=>4001,
                    "message"=>"验证失败，标题不能为空"
                ]);
            }
            //return json(["code"=>$post]);
            //var_dump($post);exit;
            // 判断已有内容不能重复新增去修改
            $name = Db::query("select re_id,responsiblities_cn from ls_responsiblities where`p_id`='".$post['p_id']."'and `responsiblities_cn`='".$post['responsiblities_cn']."' and delete_time is NULL ");
            //var_dump($name);exit;
            if(!empty($name)){
                return json([
                    "code"=>4000,
                    "message"=>"该资质已存在"
                ]);
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');
            //$model->insert($post);
            unset($post["rename_cn"],$post["rename_en"]);
            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);

            //var_dump($model);
            //var_dump($line);die();
            if(!$line){
                return json([
                    "code"=>4002,
                    "message"=>"添加失败"
                ]);
            } else {
                return json([
                    "code"=>2000,
                    "data"=>['id'=>$model->re_id],
                    "message"=>"添加成功"
                ]);

            }
        };

    }

    //修改责任
    public function re_update(){
        if (Request::instance()->isAjax())
        {
            $model = new ResponsiblitiesModel;
            if(Request::instance()->isPost()){
                $post =Request::instance()->post() ;// 获取经过过滤的全部post变量

                if(!empty($post["responsiblities_cn"])){
                    $post["responsiblities_cn"]= addslashes($post["responsiblities_cn"]);
                    $post["responsiblities_en"]= addslashes($post["responsiblities_en"]);
                    //验证表单并插入数据库
                    $result = $this->validate($post,'Responsiblities.edit');
                    if(true !== $result){
                        return json([
                            "code"=>4001,
                            "message"=>"验证失败，标题不能为空"
                        ]);
                    }
                    unset($post["rename_cn"],$post["rename_en"]);
                    // 过滤掉数据表中没有的字段，保存
                    $result = $model->allowField(true)->save($post,['re_id'=>$post["re_id"]]);
                    //echo $result;exit;
                    if(false === $result){
                        return json([
                            "code"=>4002,
                            "message"=>"添加失败"
                        ]);
                    } else {
                        return json([
                            "code"=>2000,
                            "message"=>"修改成功"
                        ]);
                    }
                }else{
                    $record = ResponsiblitiesModel::get($post["re_id"]);
                    return json($record);
                }
            }
        }
    }

    //删除责任
    public function re_delete(){
        if (Request::instance()->isAjax())
        {
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            ResponsiblitiesModel::destroy($post["re_id"]);

            return json(
                [
                    "code"=>2000,
                    "message"=>"删除成功"
                ]
            );
        }


    }


}